package cn.wolfcode.Controller;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryObject;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.service.SystemDictionaryItemService;
import cn.wolfcode.service.SystemDictionaryService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
   private SystemDictionaryItemService systemDictionaryItemService;
   

    @Autowired
    private SystemDictionaryService systemDictionaryService;


    /**
     * 点击编辑/新增二级联动：
     * 当我们选中字典目录时，
     * 给选中字典目录的select的绑定一个值改变事件
     * 通过这个事件来发送ajax请求到后台（以字典目录的id为参数），返回数据，然后进行数据的回显
     */
    @RequestMapping("/selectByDictionaryId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByDictionaryId(Long id){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectByPrimaryTypeId(id);
        return systemDictionaryItems;
    }

    @RequestMapping("/selectByParentId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByParentId(Long id){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectByParentId(id);
        return systemDictionaryItems;
    }






//    @RequestMapping("/list")
//    public String listAll(Model model){
//        List<SystemDictionaryItemItem> departments = departmentMapper.selectAll();
//        model.addAttribute("departments",departments);
//        return "systemDictionaryItemItem/list";
//    }

    @RequirePermission(name = "查看字典目录明细",experssion = "systemDictionaryItemItem:list")
    @RequestMapping("/list")
    public String listAll(Model model, @ModelAttribute("qo") SystemDictionaryObject queryObject){
        System.out.println(queryObject);
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.listPage(queryObject);
        List<SystemDictionary> dictionaries = systemDictionaryService.selectAll();
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectAll();
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("dictionaries",dictionaries);
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);
        return "systemDictionaryItem/list";
    }

    @RequirePermission(name = "字典目录明细删除",experssion = "systemDictionaryItemItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            systemDictionaryItemService.deleteByPrimaryKey(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }

//    @RequestMapping("/input")
//    public String input(Long id,Model model){
//        if (id != null){
//            SystemDictionaryItemItem systemDictionaryItemItem = departmentService.selectByPrimaryKey(id);
//            model.addAttribute("systemDictionaryItemItem", systemDictionaryItemItem);
//        }
//        return "systemDictionaryItemItem/input";
//    }
@RequirePermission(name = "字典目录明细权限新增/编辑",experssion = "systemDictionaryItemItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(@ModelAttribute("qo") SystemDictionaryItem systemDictionaryItem,Model model){
        if (systemDictionaryItem.getId() != null){
            systemDictionaryItemService.updateByPrimaryKey(systemDictionaryItem);
        }else {
            systemDictionaryItemService.insert(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }




}

